<?php
class TaskActController extends Zone_Action{
	public function indexAction(){
		$this->removeLayout();
		$user_id = get_user_id();
		$act = get('act',NULL);
		
		switch( $act ){
			case "unread":
				$this->unRead();
				break;
			case "read":
				$this->setRead();
				break;
		}	
	}

	protected function unRead(){
		$user_id = get_user_id();
		$ids = get('ID',array(),true);
		
		if( count($ids) > 0 ){
			foreach( $ids as $j => $v ){
				$ids[$j] = "'$v'";
			}
			$this->Model->delete("tasks_reads","`task_id` IN (".implode(',',$ids).") AND `user_id`='$user_id'");	

			if( get('back','') != 'yes' ){	
				$this->setJSON(array(
					reload	=> 'yes'
				));
			}else{
				$this->setJSON(array(
					redirect => '#Task'
				));
			}			
		}else{
			return $this->setJSON(array(
				alert	=> "Chưa chọn công việc nào"
			));
		}		
	}
	
	protected function setRead(){
		$user_id = get_user_id();
		$ids = get('ID',array(),true);

		if( count($ids) == 0 ){
			return $this->setJSON(array(
				alert	=> "Chưa chọn công việc nào"
			));
		}	
		
		foreach( $ids as $j ){
			$check = $this->Model->fetchRow("SELECT `a`.`ID`
				FROM  `tasks` as `a`
				LEFT JOIN `tasks_users` as `b`
					ON `b`.`user_id`='$user_id' AND `b`.`task_id`=`a`.`ID`
				WHERE `a`.`is_draft`='no' AND ( `b`.`user_id` IS NOT NULL OR `a`.`created_by_id`='$user_id' ) AND `a`.`ID`='$j'");
			
			if( $check ){
				if( !$this->Model->fetchRow("SELECT * FROM `tasks_reads` WHERE `task_id`='$j' AND `user_id`='$user_id'") ){
					$this->Model->insert('tasks_reads',array(
						task_id	=> $j,
						user_id		=> $user_id,
						date_read	=> mysql_date()
					));
				}	
			}
		}
		$this->setJSON(array(
			reload	=> 'yes'
		));
	}
	
	public function deleteAction(){
		$ids = get('ID',array(),true);
		$this->removeLayout();
		$user_id = get_user_id();
		$i = 0;
		
		foreach( $ids as $id ){	
			$post = $this->Model->fetchRow("SELECT * FROM `tasks` WHERE `ID`='$id' AND `is_draft`='no'");

			if( !$post || $post['created_by_id'] != $user_id ){
				continue;
			}			
	
			$i++;
			//remove all thing
			$this->Model->delete('tasks',"`ID`='$id'");
			
			$files = $this->Model->fetchAll("SELECT * FROM `tasks_files` WHERE `task_id`='$id'");
			$this->Model->delete("tasks_files","`task_id`='$id'");
			$this->Model->delete("tasks_users","`task_id`='$id'");
			$this->Model->delete("tasks_reads","`task_id`='$id'");
					
			foreach( $files as $file ){
				@unlink("files/task/{$file['name']}");
			}
					
			$comments = $this->Model->fetchAll("SELECT * FROM `tasks_comments` WHERE `task_id`='$id'");
			
			$this->Model->delete('tasks_comments',"`task_id`='$id'");
			
			foreach( $comments as $comment ){
				$files = $this->Model->fetchAll("SELECT * FROM `tasks_comments_files` WHERE `comment_id`='{$comment['ID']}'");
				foreach( $files as $file ){
					@unlink("files/task/{$file['name']}");
				}
			}			
		}

		if( $i == 0 ){
			return $this->setJSON(array(
				alert => 'Không có công việc nào được xóa'
			));		
		}
		
		return $this->setJSON(array(
			redirect => '#'.get('url','Task')
		));
	}

	public function remindAction(){
		$ids = get('ID',array(),true);
		$this->removeLayout();
		$user_id = get_user_id();
		$i = 0;
		
		foreach( $ids as $id ){	
			$post = $this
				->Model->fetchRow("SELECT * FROM `tasks` 
					WHERE `is_draft`='no' AND `ID`='$id' AND `is_draft`='no'");

			if( !$post || $post['created_by_id'] != $user_id ){		
				continue;		
			}	
			
			$users = $this
				->Model->fetchAll("SELECT `a`.* 
					FROM `users` as `a` 
					LEFT JOIN `tasks_users` as `b` 
						ON `b`.`user_id`=`a`.`ID` AND `b`.`task_id`='$id' 
					WHERE `a`.`is_deleted`='no' AND `b`.`user_id`");
			
			foreach( $users as $user ){
				$i++;
				$this->Model->insert('notices',array(
					created_by_id	=> $user_id,
					user_id	=> $user['ID'],
					url		=> "#Task/Index/View?ID=$id",
					title	=> $post['title'],
					content	=> 'Công việc được nhắc nhở',
					date	=> mysql_date()
				));
			}
		}
		$this->setJSON(array(
			alert => "Đã gửi thành công $i nhắc nhở"
		));				
	}	
}	